<template>
	<div>
		<h1>注册页面</h1>
		<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
			<el-form-item label="电话号码" prop="tel">
				<el-input type="tel" v-model="ruleForm.tel"></el-input>
				<el-button type="primary" @click="code()">点击获取手机验证码</el-button>
			</el-form-item>
			<el-form-item label="短信验证码" prop="verificationCode">
				<el-input type="text" v-model="ruleForm.verificationCode"></el-input>
			</el-form-item>
			<el-form-item label="邀请码" prop="referralCode">
				<el-input type="text" v-model="ruleForm.referralCode"></el-input>
			</el-form-item>
			<el-form-item label="密码" prop="pass">
				<el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="checkPass">
				<el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
				<el-button @click="resetForm('ruleForm')">重置</el-button>
			</el-form-item>
		</el-form>
		<h2>
			<router-link to="login">已有账号?去登陆</router-link>
		</h2>
	</div>
</template>

<script>
	import $ from "jquery"
	export default {
		data() {
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					if (this.ruleForm.checkPass !== '') {
						this.$refs.ruleForm.validateField('checkPass');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.ruleForm.pass) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				ruleForm: {
					tel: '',
					verificationCode: '',
					referralCode: '',
					pass: '',
					checkPass: '',
				},
				rules: {
					pass: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					checkPass: [{
						validator: validatePass2,
						trigger: 'blur'
					}]
				}
			};
		},
		methods: {
			code: function() {
				var _this = this;
				$.ajax({
					url: "http://localhost:80/user/code",
					type: "get",
					data: {
						tel: _this.ruleForm.tel,
					},
					success: function(data) {
						alert(data.message)
					},
					error: function(data) {
						alert(data.responseJSON.message)
					}
				});
			},
			submitForm(formName) {
				var _this = this;
				this.$refs[formName].validate((valid) => {
					if (valid) {
						$.ajax({
							url: "http://localhost:80/user/register",
							type: "post",
							data: {
								tel: _this.ruleForm.tel,
								password: _this.ruleForm.password,
								verificationCode: _this.ruleForm.verificationCode,
								referralCode: _this.ruleForm.referralCode,
							},
							success: function(data) {
								sessionStorage.setItem("Access-Token", data.message);
								// 后端登陆成功
								_this.$router.push("/map");
							},
							error: function(data) {
								alert(data.responseJSON.message)
							}
						});
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style>
</style>
